package cn.sxt.yxl.controller.head;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sun.org.apache.bcel.internal.classfile.Code;

import cn.sxt.yxl.common.controller.BaseController;
import cn.sxt.yxl.common.util.ConstatFinalUtil;
import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.question.pojo.AColumns;
import cn.sxt.yxl.question.pojo.AColumnsEnum;
import cn.sxt.yxl.question.pojo.AItems;
import cn.sxt.yxl.question.pojo.AItemsEnum;
import cn.sxt.yxl.question.service.IQuestionService;

/**首页模块的controller
 * @param <K>*/
@Controller
public class IndexHeadController extends BaseController {
	
	@Resource
	private IQuestionService questionService;
	
	/**
	 * 首页
	 * */
	@RequestMapping("/main")
	public String main(){
		ConstatFinalUtil.LOGGER.info("---- main ----");
		return "/head/main";
	}
	/**栏目下面的测试项列表页面*/
	@RequestMapping("/{url}")
	public String itemsList(HttpServletRequest request,@PathVariable String url){
		ConstatFinalUtil.LOGGER.info("-----itemList--- url:{} ---",url);
		Map<String ,Object> condMap = new HashMap<String ,Object>();
		
		/* 查询url对应的栏目 */
		condMap.clear();
		condMap.put("url", url);
		AColumns columns = this.questionService.findOneColumnsService(condMap);
		request.setAttribute("columns", columns);
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 查询url下面对应的测试项信息 */
		/* 启用 */
		condMap.clear();
		condMap.put("columns", columns.getId());
		condMap.put("status", AItemsEnum.STATUS_ENABLE.getStatus());
		List<AItems> itemsList = this.questionService.findItemsCondListService(pageInfoUtil, condMap);
		/* 测试项列表 */
		request.setAttribute("pageInfoUtil",pageInfoUtil);
		request.setAttribute("itemsList", itemsList);
		return "/head/itemsList";
	}
	
	/**测试项的详情页面*/
	@RequestMapping("/items/{iid}")
	public String itemsInfo(HttpServletRequest request,@PathVariable int iid ){
		ConstatFinalUtil.LOGGER.info("----------itemsInfo---------");
		/* 根据id查询测试项 */
		Map<String ,Object> condMap = new HashMap<String ,Object>();
		condMap.clear();
		condMap.put("id", iid);
		AItems items = this.questionService.findOneItemsService(condMap);
		/* 将结果放到request */
		request.setAttribute("items", items);
		/* 查询上一条 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		condMap.clear();
		condMap.put("pubTimeDa", items.getPubTime());
		condMap.put("orderby", "orderbyASC");
		List<AItems> itemsPreList = this.questionService.findItemsCondListService(pageInfoUtil, condMap);
		request.setAttribute("itemsPreList", itemsPreList);
		
		/* 查询下一条 */
		condMap.clear();
		condMap.put("pubTimeXiao", items.getPubTime());
		List<AItems> itemsNextList = this.questionService.findItemsCondListService(pageInfoUtil, condMap);
		request.setAttribute("itemsNextList", itemsNextList);
		
		return "/head/itemsInfo";
	}
	
	/**问题列表页面*/
	@RequestMapping("/items/{iid}/start")
	public String questionList(HttpServletRequest request,@PathVariable int iid){
		ConstatFinalUtil.LOGGER.info("--------questionList--------");
		return "/head/questionList";
	}
	
	/**查询所有的栏目**/
	@RequestMapping("/header")
	public String header(HttpServletRequest request){
		/* 接收一个参数 */
		String id = request.getParameter("id");
		ConstatFinalUtil.LOGGER.info("---- header ---- id:{} ---",id);
		
		/* 将所有栏目查出来 */
		Map<String , Object> condMap =new HashMap<String, Object>();
		
		/* 只查询状态为启动的 */
		condMap.put("status", AColumnsEnum.STATUS_ENABLE.getStatus());
		List<AColumns> columnsList = this.questionService.findCondListColumnsService(null, condMap);
		
		/* 将查询结果放到request中 */
		request.setAttribute("columnsList", columnsList);
		return "/head/header";
	}
	
	
}
